// Author: Mark Richardson
// Date: 01/26/2022
// Revised date: 08/22/2022 - to make standard deviation on naive prior a parameter passed from R
// Multi-rater item response model for latent traits with naive prior on latent latent traits
// Proofed 11/09/2023

data {
  
  int<lower = 0> N; // number of ratings
  
  int<lower = 0> A; // number of agencies
  
  int<lower = 0> R; // number of raters
  
  int<lower = 1, upper = A> aa[N]; // agency index
  
  int<lower = 1, upper = R> rr[N]; // rater index
  
  vector[N] y; // vector of ratings
  
  real<lower = 0> sd_naive; // Naive prior standard deviation
  
}


parameters {
  
  real mu_alpha; // grand mean for rater intercepts
  
  vector[R] alpha; // rater intercept parameters
  
  vector<lower = 0>[R] beta; // rater slope parameters

  vector[A] x; // latent performance

  real<lower = 0> sigma; // scale of latent performance
  
  real<lower = 0> sigma_alpha; // scale of rater intercepts
  
  real<lower = 0> sigma_beta; // scale of rater slopes
  
}


model {
  
  alpha ~ normal(0, sigma_alpha);
  
  beta ~ normal(0, sigma_beta);
  
  x ~ normal(0, sd_naive); // Naive prior on latent trait
  
  mu_alpha ~ normal(0, 5);
  
  sigma ~ normal(0, 5);
  
  sigma_alpha ~ normal(0, 5);
    
  sigma_beta ~ normal(0, 5);
  
  for (n in 1:N) {
    
    y[n] ~ normal((mu_alpha + alpha[rr[n]]) + beta[rr[n]] * x[aa[n]], sigma);

  }
}

